﻿Imports System.Data
Imports VSModel
Imports Cls_Common

Partial Class Control_StatusActionRule_Edit
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "jqueryEnable", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "jqueryEnable", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
            getFunction()
            If Not Request.QueryString("Id").ToString.Equals("0") Then
                hidID.Value = Request.QueryString("Id")
                ShowData()
            End If
        End If
    End Sub

    Protected Sub getFunction()
        ddlFunction.Items.Clear()
        Dim data As New VSEntities
        Dim p As List(Of SysFunction) = (From q In data.SysFunctions Select q).ToList
        ddlFunction.DataValueField = "FunctionId"
        ddlFunction.DataTextField = "FunctionName"
        ddlFunction.DataSource = p
        ddlFunction.DataBind()
        ddlFunction.Items.Insert(0, New ListItem("--- Chọn ---", "0"))
        ddlAction.Items.Insert(0, New ListItem("--- Chọn ---", "0"))
        ddlNextStatus.Items.Insert(0, New ListItem("--- Chọn ---", "0"))
        ddlStatus.Items.Insert(0, New ListItem("--- Chọn ---", "0"))
    End Sub

    Protected Sub getActionByFunction()
        ddlAction.Items.Clear()
        Dim data As New VSEntities
        Dim p As List(Of Action) = (From q In data.Actions Where q.FunctionId = ddlFunction.SelectedValue Select q).ToList
        ddlAction.DataValueField = "ActionId"
        ddlAction.DataTextField = "Title"
        ddlAction.DataSource = p
        ddlAction.DataBind()
        ddlAction.Items.Insert(0, New ListItem("--- Chọn ---", "0"))
    End Sub
    Protected Sub getStatusByFunction()
        ddlStatus.Items.Clear()
        Dim data As New VSEntities
        Dim p As List(Of Status) = (From q In data.Status Where q.FunctionId = ddlFunction.SelectedValue Select q).ToList
        ddlStatus.DataValueField = "StatusId"
        ddlStatus.DataTextField = "Title"
        ddlStatus.DataSource = p
        ddlStatus.DataBind()
        ddlStatus.Items.Insert(0, New ListItem("--- Chọn ---", "0"))
    End Sub
    Protected Sub getNextStatusByFunction()
        ddlNextStatus.Items.Clear()
        Dim data As New VSEntities
        Dim p As List(Of Status) = (From q In data.Status Where q.FunctionId = ddlFunction.SelectedValue Select q).ToList
        ddlNextStatus.DataValueField = "StatusId"
        ddlNextStatus.DataTextField = "Title"
        ddlNextStatus.DataSource = p
        ddlNextStatus.DataBind()
        ddlNextStatus.Items.Insert(0, New ListItem("--- Chọn ---", "0"))
    End Sub

    Protected Sub ShowData()
        Using data As New VSEntities
            Dim p As StatusAction = (From q In data.StatusActions Where q.Id = hidID.Value Select q).SingleOrDefault
            If Not p Is Nothing Then
                ddlFunction.SelectedValue = IIf(IsNothing(p.FunctionId) = True, "0", p.FunctionId)
                getActionByFunction()
                getStatusByFunction()
                getNextStatusByFunction()
                ddlStatus.SelectedValue = IIf(IsNothing(p.CurrentStatusId) = True, "0", p.CurrentStatusId)
                ddlAction.SelectedValue = IIf(IsNothing(p.ActionId) = True, "0", p.ActionId)
                ddlNextStatus.SelectedValue = IIf(IsNothing(p.NextStatusId) = True, "0", p.NextStatusId)
            End If
        End Using
    End Sub
    Protected Function checkExist() As Boolean
        Using data As New VSEntities
            Dim intId As Integer = CType(hidID.Value, Integer)
            Dim p = (From q In data.StatusActions Where q.FunctionId = ddlFunction.SelectedValue And q.CurrentStatusId = ddlStatus.SelectedValue And q.ActionId = ddlAction.SelectedValue And q.Id <> intId Select q).FirstOrDefault
            If p Is Nothing Then
                Return True
            Else
                Return False
            End If
        End Using
    End Function
#End Region
#Region "Event for control"
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            Dim p As StatusAction = (From q In data.StatusActions Where q.Id = hidID.Value).SingleOrDefault
            Try
                p.CurrentStatusId = ddlStatus.SelectedValue
                p.FunctionId = ddlFunction.SelectedValue
                p.ActionId = ddlAction.SelectedValue
                p.NextStatusId = ddlNextStatus.SelectedValue
                p.Id = hidID.Value
                Data.SaveChanges()
                'Insert_App_Log("Insert StatusActionRule:" & txtTitle.Text.Trim & "", Function_Name.StatusActionRule, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/StatusActionRule/List.aspx';", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub
    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click

        Response.Redirect("List.aspx")
    End Sub
   
    Protected Sub ddlFunction_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlFunction.SelectedIndexChanged
        getActionByFunction()
        getStatusByFunction()
        getNextStatusByFunction()
    End Sub
#End Region
End Class
